package com.tywj.buscustomerapp.view.myview;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.daimajia.numberprogressbar.NumberProgressBar;
import com.tywj.buscustomerapp.R;

public class TaylorPopupWindow1 extends PopupWindow {

    private NumberProgressBar progress;
    private Activity context;
    private View contentView;

    public TaylorPopupWindow1(Context context) {
        super(context);
        this.context = (Activity) context;
        this.initPopupWindow();
    }

    public TaylorPopupWindow1(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TaylorPopupWindow1(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    private void initPopupWindow() {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contentView = inflater.inflate(R.layout.popuwindow_layout1, null);
        this.setContentView(contentView);
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
        ColorDrawable dw = new ColorDrawable(context.getResources().getColor(R.color.whitegray));
        this.setBackgroundDrawable(dw);
        this.setContentView(contentView);
        progress = contentView.findViewById(R.id.progress);
        this.setAnimationStyle(android.R.style.Animation_Dialog);
    }

    public void setProgress(int p){
        progress.setProgress(p);
    }

    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);
        } else {
            this.dismiss();
        }
    }

    public void showAtDropDownCenter(View parent) {
        if (parent.getVisibility() == View.GONE) {
            this.showAtLocation(parent, 0, 0, 0);
        } else {
            this.showAtLocation(parent, 0, 0,0);
        }
    }




    @Override
    public void showAsDropDown(View anchor) {
        if(Build.VERSION.SDK_INT >= 24){
            Rect visibleFrame = new Rect();
            anchor.getGlobalVisibleRect(visibleFrame);
            int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
            setHeight(height);
        }
        super.showAsDropDown(anchor);
    }

}
